package com.softa.service.system.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageInfo;
import com.softa.dao.model.SysResource;
import com.softa.dao.model.SysRoleres;
import com.softa.service.system.ResourceService;
import com.softa.service.system.RoleResService;
import com.softa.vo.DataGridRequest;
import com.softa.vo.QueryRequest;
import org.springframework.stereotype.Service;

import com.softa.annotation.ServiceLog;
import com.softa.bean.DataGridModel;
import com.softa.dao.mapper.base.SysRoleMapper;
import com.softa.dao.mapper.relation.system.SysRoleRelationMapper;
import com.softa.dao.model.SysRole;
import com.softa.service.system.RoleService;

/**
 * 
* @ClassName: RoleServiceImpl
* @Description: TODO
* @author yangl
* @date 2015-6-3 下午1:32:21
*
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Resource
    private SysRoleMapper sysRoleMapper;
	@Resource
    private SysRoleRelationMapper sysRoleRelationMapper;
	@Resource
    private ResourceService resourceService;
	@Resource
	private RoleResService roleResService;

	@ServiceLog("新增角色")
    public String createRole(SysRole role) {
		DataGridModel dataGrid = new DataGridModel();
    	dataGrid.getParams().put("name", role.getName());
        boolean isName=this.isExists(dataGrid);
    	String str="录入成功";
    	if(isName){
    		str="1";//存在名称
    	}else{
    		dataGrid.getParams().put("name", "");
    		dataGrid.getParams().put("role", role.getRole());
    		boolean isCode=this.isExists(dataGrid);
    		if(isCode){
    			str="2";//存在code
        	}else{
				role.setAvailable(true);
        		sysRoleMapper.insert(role);
        	}
    	}
        return str;
    }

	@ServiceLog("更新角色")
    public String updateRole(SysRole role) {
		DataGridModel dataGrid = new DataGridModel();
		dataGrid.getParams().put("id", role.getId());
    	dataGrid.getParams().put("name", role.getName());
        boolean isName=this.isExists(dataGrid);
     	String str="修改成功";
     	if(isName){
     		str="1";//存在名称
     	}else{
        	dataGrid.getParams().put("name", "");
        	dataGrid.getParams().put("role", role.getRole());
     		boolean isCode=this.isExists(dataGrid);
     		if(isCode){
     			str="2";//存在code
         	}else{
         		sysRoleMapper.updateByPrimaryKeySelective(role);
         	}
     	}
         return str;
    }
	
	public boolean isExists(DataGridModel dataGrid){
    	List<SysRole> list=sysRoleRelationMapper.selectByParam(dataGrid);
    	boolean flag=false;
    	if(list.size() > 0){
    		flag=true;
    	}
    	return flag;
    }

	@ServiceLog("删除角色")
    public void deleteRole(Long roleId) {
		sysRoleMapper.deleteByPrimaryKey(roleId);
		roleResService.deleteRoleResByRoleId(roleId);
    }

    
    public SysRole findOne(Long roleId) {
        return sysRoleMapper.selectByPrimaryKey(roleId);
    }
    
    public SysRole findOne(String code) {
    	DataGridModel dataGrid = new DataGridModel();
    	dataGrid.getParams().put("role", code);
    	List<SysRole> roleList=sysRoleRelationMapper.selectByParam(dataGrid);
    	SysRole role=null;
    	if(roleList != null && roleList.size() > 0){
    		role=roleList.get(0);
    	}
        return role;
    }

    
    public List<SysRole> findAll() {
        return sysRoleRelationMapper.selectByParam(new DataGridModel());
    }


    @Override
	public Set<String> findAllRoles() {
		Set<String> roles = new HashSet<String>();
		List<SysRole> list = findAll();
		for(SysRole role : list) {
			if(role != null) {
				roles.add(role.getRole());
			}
		}
		return roles;
	}

    
    public Set<String> findRoles(String... roleIds) {
        Set<String> roles = new HashSet<String>();
        for(String roleId : roleIds) {
        	SysRole role = findOne(roleId);
            if(role != null) {
                roles.add(role.getRole());
            }
        }
        return roles;
    }

    
    public Set<String> findPermissions(String[] roleIds) {
        Set<Long> resourceIds = new HashSet<Long>();
        for(String roleId : roleIds) {
        	SysRole role = findOne(roleId);
            if(role != null) {
                resourceIds.addAll(this.getListResourceIds(role.getId()));
            }
        }
        return resourceService.findPermissions(resourceIds);
    }
    /**获取菜单id*/
    public List<Long> getListResourceIds(Long roleId){
    	 List<Long> list=new ArrayList<Long>();
    	 List<SysRoleres> roleResList=roleResService.queryRoleResListByRoleId(roleId);
    	 for(SysRoleres r:roleResList){
    		 list.add(r.getResId());
    	 }
	     return list;
    }

    @Override
	public List<SysResource> findResourceList(String[] roleIds) {
    	List<SysResource> list = new ArrayList<SysResource>();
        for(String roleId : roleIds) {
            SysRole role = findOne(roleId);
            if(role != null) {
            	list.addAll(this.getListResource(role.getId()));
            }
        }
		return list;
	}
    
    /**获取菜单List*/
    public List<SysResource> getListResource(Long roleId){
    	 List<SysResource> list=new ArrayList<SysResource>();
    	 List<SysRoleres> roleResList=roleResService.queryRoleResListByRoleId(roleId);
    	 for(SysRoleres r:roleResList){
    		 SysResource rr=resourceService.findOne(r.getResId());
    		 if(rr.getType().equals("menu")){
    			 list.add(rr);
    		 }
    	 }
	     return list;
    }
    
	@Override
	public PageInfo<SysRole> queryRoleByParam(DataGridRequest request, final QueryRequest queryRequest) {
		return  request.startPagePlugin().doSelectPageInfo(new ISelect() {
			@Override
			public void doSelect() {
				sysRoleRelationMapper.selectByCondition(queryRequest);
			}
		});
	}

	@Override
	public Long queryRoleTotalByParam(DataGridModel dataGrid) {
		return sysRoleRelationMapper.selectCountByCondition(dataGrid);
	}

}
